package de.koller.worlders.engine.tiles.light;

import java.util.Iterator;

public class MDistanceIterator implements Iterator<int[]> {

	private final int distance;
	private int y;
	private int o = 0;
	
	public static Iterable<int[]> create( final int distance ) {
		return new Iterable<int[]>() {
			@Override public Iterator<int[]> iterator() {
				return new MDistanceIterator( distance );
			}
		};
	}
	
	private MDistanceIterator( int distance ) {
		if( distance < 0 )
			throw new IllegalArgumentException( "distance have to be >=0." );
		
		this.distance = distance;
		this.y = -distance-1;
	}
	
	@Override
	public boolean hasNext() {
		return distance > y;
	}

	@Override
	public int[] next() {
		if( o == 0 ) { 
			y++; 
		}
		
		int x = distance-Math.abs( y );
		if( o == 0 ) {
			x *= -1;
			o++;
		} else if( o == 1 ) {
			o = 0;
		}
		
		int[] w = new int[] { x, y };
		return w;
	}
	
	@Override
	public void remove() {
		throw new UnsupportedOperationException( "Not supported yet." );
	}
	
}
